import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/Constant.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/ImageHelper.dart';

import 'Attention.dart';
import 'Recommend.dart';

//首页
class Home extends StatefulWidget {

  @override
  _HomeState createState() => _HomeState();
}

ValueNotifier<HomeHeadBgNotifier> homeStateNotifier=ValueNotifier(HomeHeadBgNotifier(ConstantColors.mainColor));

class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
  late TabController controller;
  late PageController pageController;

  @override
  void initState() {
    super.initState();
    controller = TabController(initialIndex: 1, length: 2, vsync: this)..addListener(() {
      homeStateNotifier.value.isCurrent=controller.index==1;
      homeStateNotifier.notifyListeners();
    });
    pageController=PageController(initialPage: 1)..addListener(() {

    });
  }

  // @override
  // Widget build(BuildContext context) {
  //   return ValueListenableBuilder<HomeHeadBgNotifier>(
  //     valueListenable: homeStateNotifier,
  //     builder: ( context,value, _){
  //        return Container(
  //          color: Color(ConstantColors.mainBackgroundColor),
  //          child: Stack(
  //              children: [
  //                Offstage(
  //                  offstage: !value.isChangeColor(),
  //                  child: Container(
  //                    decoration: BoxDecoration(
  //                        gradient: LinearGradient(colors: [value.isChangeColor()?Color(value.color):Colors.white,Colors.white],begin: Alignment.topCenter,end: Alignment.bottomCenter)
  //                    ),
  //                    child: Image.asset(assetPath("img_home_bg_top.png"),
  //                      height: 220.h,
  //                      alignment: Alignment.topRight,
  //                    ),
  //                  ),
  //                ),
  //                Column(
  //                  children: [
  //                    Container(
  //                      width: double.infinity,
  //                      alignment: Alignment.center,
  //                      padding: EdgeInsets.fromLTRB(0, ScreenUtil().statusBarHeight+5.h, 0, 0),
  //                      color:value.isChangeColor()?  Colors.transparent:Colors.white  ,
  //                      child: SizedBox(
  //                        height: 42.h,
  //                        width: 180.w,
  //                        child: TabBar(
  //                           // onTap: (index){
  //                           //   pageController.animateToPage(index, duration: Duration(milliseconds: 200), curve:Curves.ease );
  //                           // },
  //                           controller: controller,
  //                            indicatorSize: TabBarIndicatorSize.label,
  //                            labelColor:
  //                            value.isChangeColor()?  Colors.white:Colors.black ,
  //                            unselectedLabelStyle: TextStyle(fontSize: 16.sp),
  //                            labelStyle: TextStyle(fontSize: 20.sp),
  //                            indicatorColor: Color(ConstantColors.mainColor),
  //                            tabs: [Tab(text: "关注"), Tab(text: "推荐")]),
  //                      ),
  //                    ),
  //                    Expanded(
  //                        child: TabBarView(controller: controller,
  //                            children: [Attention(), AutoRecommend()])
  //                      // child: PageView.builder(
  //                      //    controller: pageController,
  //                      //    onPageChanged: (index){
  //                      //       controller.animateTo(index);
  //                      //    },
  //                      //    itemCount: 2,
  //                      //    itemBuilder: (BuildContext context, int index){
  //                      //      return index==0?Attention():AutoRecommend();
  //                      // }),
  //                    )
  //                  ],
  //                ),
  //              ]),
  //        );
  //     },
  //   );
  // }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color(ConstantColors.mainBackgroundColor),
      child: Stack(
          children: [
              ValueListenableBuilder<HomeHeadBgNotifier>(
                valueListenable: homeStateNotifier,
                  builder: ( context,value, _){
                    return Offstage(
                      offstage: !value.isChangeColor(),
                      child: Container(
                        decoration: BoxDecoration(
                            gradient: LinearGradient(colors: [value.isChangeColor()?Color(value.color):Colors.white,Colors.white],begin: Alignment.topCenter,end: Alignment.bottomCenter)
                        ),
                        child: Image.asset(assetPath("img_home_bg_top.png"),
                          height: 220.h,
                          alignment: Alignment.topRight,
                        ),
                      ),
                    );
                  },
                ),
              Column(
                  children: [
                    ValueListenableBuilder<HomeHeadBgNotifier>(
                      valueListenable: homeStateNotifier,
                      builder: ( context,value, _){
                        return Container(
                          width: double.infinity,
                          alignment: Alignment.center,
                          padding: EdgeInsets.fromLTRB(0, ScreenUtil().statusBarHeight+5.h, 0, 0),
                          color:value.isChangeColor()?  Colors.transparent:Colors.white  ,
                          child: SizedBox(
                            height: 42.h,
                            width: 180.w,
                            child: TabBar(
                                onTap: (index){
                                  pageController.animateToPage(index, duration: Duration(milliseconds: 200), curve:Curves.ease );
                                },
                                controller: controller,
                                indicatorSize: TabBarIndicatorSize.label,
                                labelColor:
                                value.isChangeColor()?  Colors.white:Colors.black ,
                                unselectedLabelStyle: TextStyle(fontSize: 16.sp),
                                labelStyle: TextStyle(fontSize: 20.sp),
                                indicatorColor: Color(ConstantColors.mainColor),
                                tabs: [Tab(text: "关注"), Tab(text: "推荐")]),
                          ),
                        );
                      },
                    ),
                    Expanded(
                        // child: TabBarView(controller: controller,
                            // children: [Attention(), AutoRecommend()])
                      child: PageView.builder(
                         controller: pageController,
                         onPageChanged: (index){
                            controller.animateTo(index);
                         },
                         itemCount: 2,
                         itemBuilder: (BuildContext context, int index){
                           return index==0?Attention():AutoRecommend();
                      }),
                    )
                  ],
            ),
          ]),
    );
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

}

class HomeHeadBgNotifier {

  int color;
  bool isTop;//是否置顶
  bool isCurrent; //是否当前

  HomeHeadBgNotifier(this.color, {this.isTop=true, this.isCurrent=true});

  bool isChangeColor(){
    return isTop && isCurrent;
  }
}

